package yueyong.salarymanagement.util;

import com.google.common.collect.Lists;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import yueyong.salarymanagement.module.business.salary.controller.SalaryFileController;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * Copyright: yueyong (c) 2022
 *
 * @author he.shaorong
 * @version 1.0
 * @date 2022/3/5
 */
public class SendWxMessageUtil {


    private static final Logger logger = LoggerFactory.getLogger(SendWxMessageUtil.class);

    public static final String TEMPLATE_ID = "dF7aW503VeAbfmD9d-jkglspqjIu-WIdf5OkibIKz_I";
    public static final String LINK_URL = "https://wx.yyysmart.com";
    public static String APP_ID = "wx2217b39a45be69e2";
    public static String SECRET = "e23b871fbce24875013e61f8e28f841d";
    private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月");
    private static DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分SS秒");

    public static Pair<Boolean, String> sendMessage(String employeeName, String companyName, LocalDate date, String job, String openId) {
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(APP_ID);
        wxStorage.setSecret(SECRET);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);

        List<WxMpTemplateData> templateDataList = buildTemplateDataList(employeeName, companyName, date, job);

        try {
            if (openId != null) {

                WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                        .toUser(openId)//要推送的用户openid
                        .templateId(TEMPLATE_ID)//模版id
                        .url(LINK_URL)//点击模版消息要访问的网址
                        .data(templateDataList)
                        .build();

                String result = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
                logger.info("Send Wx meassage," + result);
                System.out.println(result);
            }
        } catch (Exception e) {
            logger.error("Send Wx meassage error," + e.getMessage());
            return Pair.of(false, e.getMessage());
        }

        return Pair.of(true, "ok");
    }

    /*
        {{first.DATA}}
    薪资月份：{{keyword1.DATA}}
    岗位名称：{{keyword2.DATA}}
    服务企业：{{keyword3.DATA}}
    发放时间：{{keyword4.DATA}}
    {{remark.DATA}}
     */
    public static List<WxMpTemplateData> buildTemplateDataList(String employeeName, String companyName, LocalDate date, String job) {
        List<WxMpTemplateData> templateDataList = Lists.newArrayList();

        WxMpTemplateData wxMpTemplateData1 = new WxMpTemplateData();
        wxMpTemplateData1.setValue(String.format("尊敬的%s您好，您的工资单已生成，请点击查看", employeeName));
        wxMpTemplateData1.setColor("#173177");
        wxMpTemplateData1.setName("first");
        templateDataList.add(wxMpTemplateData1);

        WxMpTemplateData wxMpTemplateData3 = new WxMpTemplateData();
        wxMpTemplateData3.setValue(date.format(formatter));
        wxMpTemplateData3.setColor("#173177");
        wxMpTemplateData3.setName("keyword1");
        templateDataList.add(wxMpTemplateData3);

        WxMpTemplateData wxMpTemplateData4 = new WxMpTemplateData();
        wxMpTemplateData4.setValue(job);
        wxMpTemplateData4.setColor("#173177");
        wxMpTemplateData4.setName("keyword2");
//        templateDataList.add(wxMpTemplateData4);


        WxMpTemplateData wxMpTemplateData2 = new WxMpTemplateData();
        wxMpTemplateData2.setValue(companyName);
        wxMpTemplateData2.setColor("#173177");
        wxMpTemplateData2.setName("keyword3");
        templateDataList.add(wxMpTemplateData2);

        WxMpTemplateData wxMpTemplateData5 = new WxMpTemplateData();
        LocalDateTime now = LocalDateTime.now();
        wxMpTemplateData5.setValue(now.format(timeFormatter));
        wxMpTemplateData5.setColor("#173177");
        wxMpTemplateData5.setName("keyword4");
        templateDataList.add(wxMpTemplateData5);

        WxMpTemplateData wxMpTemplateData6 = new WxMpTemplateData();
        wxMpTemplateData6.setValue("感谢你的使用。");
        wxMpTemplateData6.setColor("#173177");
        wxMpTemplateData6.setName("remark");
        templateDataList.add(wxMpTemplateData6);
        return templateDataList;
    }


}
